<template>
  <div>
    <el-row>
      <el-card shadow="hover" style="padding-bottom: 30px; margin-bottom: 30px">
        <el-col :span="10">
          <div class="block">
            <span class="demonstration">创建时间:&nbsp;&nbsp;&nbsp;</span>
            <el-date-picker
              type="datetimerange"
              range-separator="至"
              start-placeholder="创建时间"
              end-placeholder="创建时间"
              value-format="yyyy-MM-dd HH:mm:ss"
              @change="optTimeHandle"
              v-model="optTime"
            >
            </el-date-picker>
          </div>
        </el-col>
        <el-col :span="4">
          <el-input
            placeholder="昵称/账号"
            v-model="queryCondition.accountAndNickName"
            clearable
          >
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" icon="el-icon-search" @click="selInfo"
            >搜索</el-button
          >
        </el-col>
        <el-col :span="2">
          <el-button
            type="primary"
            icon="el-icon-refresh"
            @click="cleanCondition"
          ></el-button>
        </el-col>
      </el-card>
    </el-row>
    <el-row style="margin-top: 20px">
      <el-col :span="3" :offset="21">
        <el-button size="mini" type="primary" @click="addNewUserBtn"
          >添加新用户</el-button
        >
      </el-col>
    </el-row>
    <el-row>
      <el-col>
        <el-table :data="tableData" stripe style="width: 100%">
          <!-- <el-table-column prop="roleName" label="角色名称"> </el-table-column> -->
          <!-- <el-table-column prop="remark" label="描述"> </el-table-column> -->
          <el-table-column prop="nickName" label="用户昵称"> </el-table-column>
          <el-table-column prop="createTime" label="创建时间">
          </el-table-column>
          <el-table-column prop="account" label="用户名"> </el-table-column>
          <el-table-column prop="" label="操作">
            <template slot-scope="scope">
              <el-link type="primary" @click="updateRole(scope.row)"
                >设置角色&nbsp;&nbsp;</el-link
              >
              <el-link type="primary" @click="updatePwd(scope.row)"
                >设置密码&nbsp;&nbsp;</el-link
              >
              <el-link type="primary" @click="deleteUser(scope.row)"
                >删除</el-link
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!-- 分页 -->
    <el-row>
      <el-col :span="7" :offset="15">
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[2, 4, 5, 8, 10, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @next-click="nextPage"
            @prev-click="prevPage"
          >
          </el-pagination>
        </div>
      </el-col>
    </el-row>

    <!-- 添加用户框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-row style="margin-bottom: 20px">
        <el-col :span="10">
          <el-input placeholder="账号" v-model="addParams.account" clearable>
          </el-input>
        </el-col>
      </el-row>
      <el-row style="margin-bottom: 20px">
        <el-col :span="10">
          <!-- <el-select
            v-model="addParams.roleId"
            clearable
            placeholder="选择角色"
          >
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.roleName"
              :value="item.id"
            >
            </el-option>
          </el-select> -->
        </el-col>
      </el-row>
      <el-row style="margin-bottom: 20px">
        <el-col :span="10">
          <el-input placeholder="昵称" v-model="addParams.nickName" clearable>
          </el-input>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 密码框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible2" width="30%">
      <el-row>
        <el-col :span="10">
          <el-input
            placeholder="请输入密码"
            v-model="updatePwdParams.pwd"
            show-password
          ></el-input>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="submitUpdate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 角色设置框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible3" width="30%">
      <el-row>
        <el-col>
          <el-table :data="roleInfo" style="width: 100%">
            <el-table-column prop="roleName" label="角色名称">
            </el-table-column>
            <el-table-column prop="remark" label="描述"> </el-table-column>
            <el-table-column prop="" label="操作">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.isChecked"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  @change="updateManageRole(scope.row)"
                >
                </el-switch>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <!-- <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible3 = false">取 消</el-button>
        <el-button type="primary" @click="submitUpdateRole">确 定</el-button>
      </span> -->
    </el-dialog>
  </div>
</template>

<script>
export default {
  mounted() {
    window.vue = this;
  },
  created() {
    this.selUserByCondition();
    this.selAllRoleInfo();
  },
  data() {
    return {
      updateRoleParams: {
        manageId: "",
      },
      roleInfo: [],
      updatePwdParams: {
        pwd: "",
        manageId: "",
      },
      addParams: {
        account: "",
        nickName: "",
        // roleId: "",
        manageId: "",
      },
      roleList: [],
      dialogVisible: false,
      dialogVisible2: false,
      dialogVisible3: false,
      queryCondition: {
        startTime: "",
        endTime: "",
        accountAndNickName: "",
      },
      optTime: [],
      tableData: [],
      pageNum: 1,
      pageSize: 5,
      total: null,
    };
  },
  methods: {
    updateManageRole(row) {
      // console.log(row);
      // console.log(this.updateRoleParams.manageId);
      let params = {};
      params.manageId = this.updateRoleParams.manageId;
      params.roleId = row.id;
      console.log(params);
      if (row.isChecked) {
        // 添加
        this.$axios
          .post("api/uums-server/addManageRole", params)
          .then((response) => {
            if (response.data.code == 200) {
              this.$message({
                showClose: true,
                message: "修改成功",
                type: "success",
              });
            }
          });
        console.log("添加");
      } else {
        // 删除
        this.$axios
          .post("api/uums-server/deleteManageRole", params)
          .then((response) => {
            if (response.data.code == 200) {
              this.$message({
                showClose: true,
                message: "修改成功",
                type: "success",
              });
            }
          });
      }
    },
    selAllRoleInfo() {
      // 查询所有的role
      this.$axios.get("api/uums-server/selRole").then((response) => {
        if (response.data.code == 200) {
          // console.log(response.data.data);
          this.roleInfo = response.data.data;
          this.roleInfo.forEach((e) => {
            this.$set(e, "isChecked", false);
          });
        }
      });
    },
    deleteUser(row) {
      // console.log(row.manageId);
      this.$axios
        .delete("api/uums-server/deleteUser", {
          params: { manageId: row.manageId },
        })
        .then((response) => {
          if (response.data.code == 200) {
            this.selInfo();
            this.$message({
              showClose: true,
              message: "删除成功",
              type: "success",
            });
          }
        });
    },

    /* ################################################################## */
    submitUpdateRole() {
      // console.log(this.addParams.roleId);
      // console.log(this.addParams.manageId);
      this.$axios.post("api/uums-server/updateUserRole").then((response) => {
        if (response.data.code == 200) {
        }
      });
      /* ################################################################## */
    },
    updateRole(row) {
      // console.log(row);
      // this.addParams.roleId = row.roleId;
      // this.addParams.manageId = row.manageId;
      this.updateRoleParams.manageId = row.manageId;
      this.dialogVisible3 = true;
      this.$axios
        .get("api/uums-server/selManageRoleByMangeId", {
          params: { manageId: row.manageId },
        })
        .then((response) => {
          if (response.data.code == 200) {
            const roleIds = response.data.data.map((e) => e.id);
            this.roleInfo.forEach((ee) => {
              // console.log(ee);
              ee.isChecked = false;
              if (roleIds.indexOf(ee.id) != -1) {
                ee.isChecked = true;
              }
            });
          }
        });
    },
    submitUpdate() {
      if (this.updatePwdParams.pwd == "") {
        this.$message({
          showClose: true,
          message: "密码不能为空",
          type: "error",
        });
        return;
      }
      //   console.log(this.updatePwdParams);
      let params = {};
      params.id = this.updatePwdParams.manageId;
      params.managePwd = this.updatePwdParams.pwd;
      this.$axios
        .post("api/uums-server/updateManagePwd", params)
        .then((response) => {
          if (response.data.code == 200) {
            this.dialogVisible2 = false;
            this.$message({
              showClose: true,
              message: "修改成功",
              type: "success",
            });
          }
        });
    },
    updatePwd(row) {
      this.dialogVisible2 = true;
      this.updatePwdParams.manageId = row.manageId;
      this.updatePwdParams.pwd = "";
    },
    addUser() {
      //   console.log(this.addParams);
      this.$axios
        .post("api/uums-server/addUser", this.addParams)
        .then((response) => {
          if (response.data.code == 200) {
            this.selInfo();
            this.dialogVisible = false;
            this.$message({
              showClose: true,
              message: "添加成功",
              type: "success",
            });
          }
        });
    },
    addNewUserBtn() {
      this.addParams = {
        account: "",
        nickName: "",
        roleId: "",
      };
      this.dialogVisible = true;
      this.$axios.get("api/uums-server/selRole").then((response) => {
        if (response.data.code == 200) {
          this.roleList = response.data.data;
        }
      });
    },
    cleanCondition() {
      this.queryCondition = {
        startTime: "",
        endTime: "",
        accountAndNickName: "",
      };
      this.optTime = [];
    },
    selInfo() {
      this.selUserByCondition();
    },
    optTimeHandle() {
      //   console.log(this.optTime);
      this.queryCondition.startTime = this.optTime[0];
      this.queryCondition.endTime = this.optTime[1];
    },
    selUserByCondition() {
      let params = {};
      params.pageNum = this.pageNum;
      params.pageSize = this.pageSize;
      params.param = this.queryCondition;
      this.$axios
        .post("api/uums-server/selUserByCondition", params)
        .then((response) => {
          if (response.data.code == 200) {
            // console.log(response.data.data);
            // this.pageNum = response.data.data.pageNum;
            this.total = response.data.data.total;
            // this.pageSize = response.data.data.pageSize;
            this.tableData = response.data.data.list;
          }
        });
    },
    prevPage(val) {
      this.pageNum = val;
      this.selUserByCondition();
    },
    nextPage(val) {
      this.pageNum = val;
      this.selUserByCondition();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.selUserByCondition();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.selUserByCondition();
    },
  },
};
</script>

<style scoped></style>
